package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.TransferClass;
import cn.wolfcode.crm.query.TransferClassQueryObject;
import cn.wolfcode.crm.service.ITransferClassService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("transferClass")
public class TransferClassController {
    @Autowired
    public ITransferClassService transferClassService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"transferClass:view","部门列表"},logical = Logical.OR)
    public String view() {
        return "transferClass/transferClass";
    }

    @ResponseBody
    @RequestMapping("query")
    public PageResult query(TransferClassQueryObject qo) {

        return transferClassService.query(qo);
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<TransferClass> selectAll() {
        return transferClassService.selectAll();
    }

    @RequestMapping("delete")
    @RequiresPermissions(value = {"transferClass:delete","部门删除"},logical = Logical.OR)
    @ResponseBody
    public JSONResult delete(Long id) {
        try {
            transferClassService.deleteByPrimaryKey(id);
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("删除失败");
        }
    }

    /**
     * 审核转班的申请,前台传过来转班申请的id值
     * @param transferClassId
     * @return
     */
    @RequestMapping("audit")
    @RequiresPermissions(value = {"transferClass:audit","部门删除"},logical = Logical.OR)
    @ResponseBody
    public JSONResult audit(Long transferClassId) {
        try {
            transferClassService.audit(transferClassId);
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("审核失败");
        }
    }
    @ResponseBody
    @RequestMapping("saveOrUpdate")
    @RequiresPermissions(value = {"transferClass:saveOrUpdate","部门新增或编辑"},logical = Logical.OR)
    public JSONResult saveOrUpdate(TransferClass transferClass){
        try {
            if(transferClass.getId() != null){
                transferClassService.updateByPrimaryKey(transferClass);
            }else{
                transferClassService.insert(transferClass);
            }
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("操作失败");
        }
    }
}
